package mall.dao;

import mall.model.User;
import mall.model.bo.AdminLoginBO;
import mall.model.bo.MallUpdatePwdBO;
import mall.model.bo.MallUpdateUserDataBO;
import mall.model.bo.MallUserSignupBO;
import mall.model.vo.MallShowDataVO;
import mall.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

/*
 *
 * @author Yonni
 * @version 1.8.0_201
 * @date 2020-06-01 11:33
 *
 * */
public class MallUserDaoImpl implements MalluserDao {
    @Override
    public void signup(MallUserSignupBO signupBO) {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        try {
            queryRunner.update("insert into user values (null, ?, ?, ?, ?, ?, ?)",
                    signupBO.getEmail(),
                    signupBO.getNickname(),
                    signupBO.getPwd(),
                    signupBO.getRecipient(),
                    signupBO.getAddress(),
                    signupBO.getPhone());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public User login(AdminLoginBO loginBO) {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        User query = null;
        try {
            query = queryRunner.query("select * from user where email = ? and pwd = ?",
                    new BeanHandler<User>(User.class),
                    loginBO.getEmail(), loginBO.getPwd());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return query;
    }

    @Override
    public MallShowDataVO showData(String token) {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        MallShowDataVO query = null;
        try {
            query = queryRunner.query("select id, email, nickname, recipient, address, phone from user where nickname = ?",
                    new BeanHandler<MallShowDataVO>(MallShowDataVO.class), token);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return query;
    }

    @Override
    public User queryUser(String id, String oldPwd) {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        User query = null;
        try {
            query = queryRunner.query("select * from user where id = ? and pwd = ?",
                    new BeanHandler<User>(User.class), id, oldPwd);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return query;
    }

    @Override
    public void updatePwd(MallUpdatePwdBO mallUpdatePwdBO) {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        try {
            queryRunner.update("update user set pwd = ? where id = ?",
                    mallUpdatePwdBO.getNewPwd(), mallUpdatePwdBO.getId());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void updateUserData(MallUpdateUserDataBO userDataBO) {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        try {
            queryRunner.update("update user set address = ?, nickname = ?, phone = ?, recipient = ? where id = ?",
                    userDataBO.getAddress(), userDataBO.getNickname(), userDataBO.getPhone(), userDataBO.getRecipient(),
                    userDataBO.getId());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
